﻿namespace $rootnamespace$
{
    using System;
    using System.Linq;
    using System.Threading;
    using ClewareWrapper;
    using ClewareWrapper.Invoke;

    public static class Samples
    {
        private static readonly TimeSpan WaitingTime = TimeSpan.FromMilliseconds(500);

        /// <summary>
        /// Sets the red light on, then orange, then green.
        /// </summary>
        public static void RunLowLevelRedToGreen()
        {
            // Init driver
            var obj = UsbAccessWrapper.InitObject();

            // Open all connected devices
            var devicesCount = UsbAccessWrapper.OpenCleware(obj);

            if (devicesCount > 0)
            {
                // Set the red light on (first device)
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_0, 1);
                Thread.Sleep(WaitingTime);

                // red off, orange on
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_0, 0);
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_1, 1);
                Thread.Sleep(WaitingTime);

                // orange off, green on
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_1, 0);
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_2, 1);
                Thread.Sleep(WaitingTime);

                // green off
                UsbAccessWrapper.SetSwitch(obj, 0, SwitchId.SWITCH_2, 0);
            }

            // Uninit driver
            UsbAccessWrapper.UnInitObject(obj);
        }

        /// <summary>
        /// Sets the red light on, then orange, then green.
        /// </summary>
        public static void RunMediumLevelRedToGreen()
        {
            using (var mngr = new ClewareManager())
            {
                // Open all connected devices
                var devicesCount = mngr.OpenCleware();

                if (devicesCount <= 0)
                {
                    return;
                }

                // Get the first connected device
                var firstDevice = mngr.Devices.First();

                // Set the red light on
                firstDevice.SetSwitch(SwitchId.SWITCH_0, 1);
                Thread.Sleep(WaitingTime);

                // red off, orange on
                firstDevice.SetSwitch(SwitchId.SWITCH_0, 0);
                firstDevice.SetSwitch(SwitchId.SWITCH_1, 1);
                Thread.Sleep(WaitingTime);

                // orange off, green on
                firstDevice.SetSwitch(SwitchId.SWITCH_1, 0);
                firstDevice.SetSwitch(SwitchId.SWITCH_2, 1);
                Thread.Sleep(WaitingTime);

                // green off
                mngr.SetSwitch(0, SwitchId.SWITCH_2, 0);
            }
        }

        /// <summary>
        /// Sets the red light on, then orange, then green. It can address a
        /// specific light identified uniquely by its serial number. Handy if
        /// more than one light is connected.
        /// </summary>
        /// <param name="serialNumber">Serial number of the device you want to control.</param>
        public static void RunTrafficLightLevelRedToGreen(int? serialNumber)
        {
            // Init and open all connected devices
            using (var mngr = new TrafficLightsManager())
            {
                var lights = mngr.TrafficLights;
                if (lights.Count < 0)
                {
                    return;
                }

                TrafficLight light;
                if (serialNumber.HasValue)
                {
                    light = lights[serialNumber.Value];
                }
                else
                {
                    light = lights.Values.First();
                }

                // Set the red light on (first device)
                light.Red = true;
                Thread.Sleep(WaitingTime);

                // red off, orange on
                light.SetOrangeOnly();
                Thread.Sleep(WaitingTime);

                // orange off, green on
                light.SetGreenOnly();
                Thread.Sleep(WaitingTime);

                // All off
                light.SwitchOff();
            }
        }
    }
}
